// internal/infrastructure/captcha/custom_driver.go
package captcha

import (
	"fmt"
	"image/color"
	"math/rand"

	"github.com/mojocn/base64Captcha"
)

type CustomDriverMath struct {
	*base64Captcha.DriverMath
}

func NewCustomDriverMath(height int, width int, noiseCount int, showLineOptions int, bgColor *color.RGBA, fonts []string) *CustomDriverMath {
	driver := base64Captcha.NewDriverMath(height, width, noiseCount, showLineOptions, bgColor, fonts)
	return &CustomDriverMath{DriverMath: driver}
}

func (c *CustomDriverMath) GenerateIdQuestionAnswer() (id, question, answer string) {
	// 实现一位数逻辑
	id = base64Captcha.RandomId()
	operators := []string{"+", "-", "x"}
	var mathResult int32
	op := operators[rand.Int31n(3)]
	a := rand.Int31n(10) // 限定为一位数
	b := rand.Int31n(10) // 限定为一位数

	switch op {
	case "+":
		question = fmt.Sprintf("%d+%d=?", a, b)
		mathResult = a + b
	case "x":
		question = fmt.Sprintf("%dx%d=?", a, b)
		mathResult = a * b
	default:
		if a < b {
			a, b = b, a
		}
		question = fmt.Sprintf("%d-%d=?", a, b)
		mathResult = a - b
	}
	answer = fmt.Sprintf("%d", mathResult)
	return
}
